<?php
namespace Mobile\Controller;
use Mobile\Logic\ReplyLogic;
use Think\AjaxPage;
use Think\Page;
class GoodsController extends MobileBaseController {
    public function index(){
       // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        $this->display();
    }

    /**
     * 商品详情页
     */
    public function goodsInfo(){
        C('TOKEN_ON',true);        
        $goodsLogic = new \Home\Logic\GoodsLogic();
        $goods_id = I("get.id");
        $goods = M('Goods a')
		->field('a.*,b.expression,b.start_time,b.end_time')
		->join('ty_prom_goods b on a.prom_id=b.id','LEFT')
		->where("a.goods_id = $goods_id")->find();
        if(empty($goods)){
        	$this->tp404('此商品不存在或者已下架');
        }
        if($goods['brand_id']){
            $brnad = M('brand')->where("id =".$goods['brand_id'])->find();
            $goods['brand_name'] = $brnad['name'];
        }

		$goods_images_list = M('goods_images')->field('image_url')->order('`sort_id` asc')->where("goods_id = $goods_id")->select(); // 商品 图册
		array_unshift($goods_images_list,array('image_url'=>C('MYOSS')['url2'].$goods['original_img']));// 插入主图到相册
        $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
        $goods_attr_list = M('GoodsAttr')->where("goods_id = $goods_id")->select(); // 查询商品属性表                        
		$filter_spec = $goodsLogic->get_spec($goods_id);  
         $specImage =  M('SpecImage')->where("goods_id = $goods_id and src != '' ")->getField("spec_image_id,src");
         
// 		 dump($myitem_id);
// 		 dump($keys);
// 处理商品规格
        $Model = new \Think\Model();
        $spec_goods_price  = M('spec_goods_price')->where("goods_id = $goods_id")->getField("key,price,store_count,goods_id,qipi_num"); // 规格 对应 价格 库存表

		foreach($spec_goods_price as $key=>$v){
			if (($data > $prom_goods['start_time']) && ($data < $prom_goods['end_time'])){
				$spec_goods_price[$key]['price']=\geshi($v['price']*$goods['floating']/100*($goods['expression']/100));
			}else{
				$spec_goods_price[$key]['price']=\geshi($v['price']*$goods['floating']/100);
			}
			$keys = str_replace('_',',',$v['key']);
			$spec_goods_price[$key]['src'] = M('spec_image')->where(['spec_image_id'=>['in',$keys],'goods_id'=>$v['goods_id'],'src'=>['neq','']])->getField('src');
		
		}
// 		dump($spec_goods_price);
        //M('Goods')->where("goods_id=$goods_id")->save(array('click_count'=>$goods['click_count']+1 )); //统计点击数
        $commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
        $this->assign('spec_goods_price', json_encode($spec_goods_price,true)); // 规格 对应 价格 库存表
      	$goods['sale_num'] = M('order_goods')->where("goods_id=$goods_id and is_send=1")->count();
        //商品促销
        if($goods['prom_type'] == 1)
        {
            $prom_goods = M('prom_goods')->where("id = {$goods['prom_id']} ")->find();
            $this->assign('prom_goods',$prom_goods);// 商品促销

            $goods['flash_sale'] = get_goods_promotion($goods['goods_id']);
            $flash_sale = M('flash_sale')->where("id = {$goods['prom_id']}")->find();
            $this->assign('flash_sale',$flash_sale);
        }

        $this->assign('commentStatistics',$commentStatistics);//评论概览
        $this->assign('goods_attribute',$goods_attribute);//属性值     
        $this->assign('goods_attr_list',$goods_attr_list);//属性列表
        $this->assign('filter_spec',$filter_spec);//规格参数
        $this->assign('goods_images_list',$goods_images_list);//商品缩略图
		$goods['discount'] = round($goods['shop_price']/$goods['market_price'],2)*10;
        $this->assign('goods',$goods);
        if($goods['store_id']>0){
        	$store = M('store')->where(array('store_id'=>$goods['store_id']))->find();
        	$this->assign('store',$store);
        }
        $this->display();
    }
    
    /*
     * 获取商品规格
     */
    public function goodsAttr(){
        $goods_id = I("get.goods_id",'0');
        $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
        $goods_attr_list = M('GoodsAttr')->where("goods_id = $goods_id")->select(); // 查询商品属性表
        $this->assign('goods_attr_list',$goods_attr_list);
        $this->assign('goods_attribute',$goods_attribute);
        $this->display();
    }
    /**
     * 商品搜索列表页
     */
    public function ajaxSearch()
    {

    }    
}